package com.yida.gwj.expend.outside.handler;

import java.time.LocalDateTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.yida.common.core.exception.ServiceException;
import com.yida.gwj.api.service.RemoteGwjConfigService;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.common.enums.GwjBusinessLogResult;
import com.yida.gwj.common.utils.GwjEnumUtils;
import com.yida.gwj.expend.outside.domain.GwjMaintenanceFundsBusiness;
import com.yida.gwj.expend.outside.domain.GwjMaintenanceFundsBusinessLog;
import com.yida.gwj.expend.outside.domain.message.MqBaseResult;
import com.yida.gwj.expend.outside.domain.message.PreAcceptExamineMessage;
import com.yida.gwj.expend.outside.domain.request.GwjMaintenanceFundsBusinessRequest;
import com.yida.gwj.expend.outside.enums.BusinessServerStatusEnum;
import com.yida.gwj.expend.outside.enums.BusinessStatusEnum;
import com.yida.gwj.expend.outside.enums.FlagStatusEnum;
import com.yida.gwj.expend.outside.enums.MaintenanceFundsErrorEnum;
import com.yida.gwj.expend.outside.service.IGwjMaintenanceFundsBusinessAttachmentService;
import com.yida.gwj.expend.outside.service.IGwjMaintenanceFundsBusinessLogService;
import com.yida.gwj.expend.outside.service.IGwjMaintenanceFundsBusinessService;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;

/**
 * 维修基金前服务处理程序
 *
 * @author liangp
 * @date 2023/04/10
 */
@Component
public class ExpendPreServiceHandlerExpendServicePre extends AbstractExpendServicePre {

    private final static Logger log = LoggerFactory.getLogger(ExpendPreServiceHandlerExpendServicePre.class);
    private IGwjMaintenanceFundsBusinessService maintenanceFundsBusinessService;
    private IGwjMaintenanceFundsBusinessLogService maintenanceFundsBusinessLogService;

    public ExpendPreServiceHandlerExpendServicePre() {}

    public ExpendPreServiceHandlerExpendServicePre(GwjBusinessFlagType businessFlagType, RemoteGwjConfigService remoteGwjConfigService, IGwjMaintenanceFundsBusinessService maintenanceFundsBusinessService, IGwjMaintenanceFundsBusinessLogService maintenanceFundsBusinessLogService, IGwjMaintenanceFundsBusinessAttachmentService maintenanceFundsBusinessAttachmentService) {
        super(businessFlagType, remoteGwjConfigService, maintenanceFundsBusinessService, maintenanceFundsBusinessLogService);
        this.maintenanceFundsBusinessService = maintenanceFundsBusinessService;
        this.maintenanceFundsBusinessLogService = maintenanceFundsBusinessLogService;
    }

    public void preAcceptSecondExamine(MqBaseResult mqBaseResult) {
        PreAcceptExamineMessage preAcceptExamineMessage = JSONUtil.toBean(mqBaseResult.getJsonMsg(), PreAcceptExamineMessage.class);
        if (ObjectUtil.isNull(preAcceptExamineMessage)) {
            throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_SECOND_EXAMINE) + "预受理二次审核转换失败");
        }
        try {
            preAcceptExamineMessage.checkNull();
        } catch (Exception e) {
            throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_SECOND_EXAMINE) + "校验失败");
        }
        GwjMaintenanceFundsBusiness maintenanceFundsBusiness = maintenanceFundsBusinessService.getGwjMaintenanceFundsBusiness(new GwjMaintenanceFundsBusinessRequest() {
            {
                setServerIdentification(preAcceptExamineMessage.getOutServerIdentification());
            }
        });
        GwjMaintenanceFundsBusiness business = maintenanceFundsBusinessService.updateGwjMaintenanceFundsBusiness(new GwjMaintenanceFundsBusiness() {
            {
                setCurrentOperId(preAcceptExamineMessage.getOperId());
                setCurrentOperName(preAcceptExamineMessage.getOperName());
                setStatus(BusinessStatusEnum.APPROVED.getCode());
                setBizId(maintenanceFundsBusiness.getBizId());
                setServerStatus(BusinessServerStatusEnum.NORMAL.getCode());
            }
        });
        if (ObjectUtil.isNull(business)) {
            throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_SECOND_EXAMINE) + "GwjMaintenanceFundsBusiness更新失败");
        }
        int insertFlag = maintenanceFundsBusinessLogService.insertGwjMaintenanceFundsBusinessLog(new GwjMaintenanceFundsBusinessLog() {
            {
                setAcceptTime(LocalDateTime.now());
                setStepId(business.getStepId());
                setBizId(business.getBizId());
                setOperId(preAcceptExamineMessage.getOperId());
                setOperName(preAcceptExamineMessage.getOperName());
                setContent(preAcceptExamineMessage.getContent());
                setResult(GwjBusinessLogResult.PASS.getCode());
            }
        });
        if (ObjectUtil.equal(insertFlag, FlagStatusEnum.FAL.getCode())) {
            throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.PRE_ACCEPT_RETREAT) + "GwjMaintenanceFundsBusinessLog新增失败");
        }
    }

}
